//
//  OrderHeaderView.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/14.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "OrderHeaderView.h"
#import "JHMacros.h"
#import "UIButton+LayoutEdgeInsets.h"

#define kOrderHeaderViewButton_W (self.width / 4)
@interface OrderHeaderView ()
{
    CGFloat _alpha;
}
@property(nonatomic,strong)NSMutableArray *btns;
@property(nonatomic,weak)UIButton *selectBtn;
@end

@implementation OrderHeaderView

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
    if (self) {
        
        self.layerIsGarden = YES;
        self.backgroundColor = k65ACFE;
        
        for (int i = 0; i<4; i++) {
            UIButton *btn;
            UIButton *onBtn;
            if (i > 0){
                onBtn = self.btns[i-1];
            }
            if (i == 0) {
                btn = [self bottomButton:@"图文" image:@"tuwen" selectImage:@"tuwen_S"];
            }else if (i == 1) {
                 btn = [self bottomButton:@"语音" image:@"yuyin" selectImage:@"yuyin_S"];
            }else if (i == 2) {
                 btn = [self bottomButton:@"视频" image:@"shipin" selectImage:@"shipin_S"];
            }else if (i == 3) {
                 btn = [self bottomButton:@"面对面" image:@"mianduimian" selectImage:@"mianduimian_S"];
            }
            
            [self.btns addObject:btn];
            [self addSubview:btn];
            
            CGFloat W = btn.width;
            CGFloat H = btn.height;
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                if (!onBtn) {
                    make.bottom.equalTo(self);
                    make.left.equalTo(self.mas_left);
                }else{
                    make.top.equalTo(onBtn.mas_top);
                    make.left.equalTo(onBtn.mas_right);
                }
                make.width.equalTo(@(W));
                make.height.equalTo(@(H));
            }];
        }
        
        UIButton *selectBtn = [self.btns objectAtIndex:0];
        selectBtn.selected = YES;
        self.selectBtn = selectBtn;
        
        [self textSize:self.selectBtn];
        [self.layer addSublayer:self.bottomLayer];
    }
    return self;
}

-(void)scrollProportion:(CGFloat)proportion
{
    
    CGFloat W = proportion * self.selectBtnTitleSize.width;
    CGFloat H = 5 - proportion * 5;
    
    if (W <= 5) {
        W = 5;
    }
    if (H <= 1.5) {
        H = 1.5;
    }
    CGRect frame = self.bottomLayer.frame;
    frame.origin.x = (kOrderHeaderViewButton_W / 2) - (W / 2) + ([self.btns indexOfObject:self.selectBtn] * kOrderHeaderViewButton_W);
    frame.size.width = W;
    frame.size.height = H;
    self.bottomLayer.frame = frame;
}

-(void)setAlpha:(CGFloat)alpha
{
    _alpha = alpha;
    for (UIButton *btn in self.btns) {
        btn.imageView.subviews[0].alpha = alpha;
    }
}

-(void)buttonAction:(UIButton *)btn
{
    NSInteger current = [self.btns indexOfObject:self.selectBtn];
    NSInteger new     = [self.btns indexOfObject:btn];

    if (current == new) return;
    
    ActionType type = current > new ? Left_Action : Right_Action;
    
    for (UIButton *btn in self.btns) {
        btn.selected = NO;
    }
    btn.selected = YES;
    
    self.selectBtn = btn;
    [self textSize:self.selectBtn];
    if (self.actionBlock) {
        self.actionBlock(@([self.btns indexOfObject:btn]));
    }
    
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.bottomLayer.frame;
        if (!self.layerIsGarden){
            frame.origin.x = (btn.width - self.selectBtnTitleSize.width) / 2 + btn.x;
            frame.size.width = self.selectBtnTitleSize.width;
            self.bottomLayer.frame = frame;

        }else{
            frame.origin.x = (btn.width - 5) / 2 + btn.x;
            [CALayer cancelAnimation:^{
                self.bottomLayer.frame = frame;
            }];
        }
    }];
    
}

-(void)textSize:(UIButton *)btn
{
    self.selectBtnTitleSize = [btn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
}

-(UIButton *)bottomButton:(NSString *)text image:(NSString *)image selectImage:(NSString *)selectImage
{
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.width = kOrderHeaderViewButton_W;
    button.height = 75;
    
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;

    [button setTitle:text forState:UIControlStateNormal];
    [button setTitle:text forState:UIControlStateSelected];
    [button setTitleColor:[@"cae2fb" colorWithHexString] forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
    
    [button layoutImageToTop:0];
    UIEdgeInsets imageEdge = button.imageEdgeInsets;
    imageEdge.top = imageEdge.top - 10;
    button.imageEdgeInsets = imageEdge;
    
    UIEdgeInsets titleEdge = button.titleEdgeInsets;
    titleEdge.bottom = titleEdge.bottom + 5;
    button.titleEdgeInsets = titleEdge;
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    /*解决button 设置select的时候 alpha重置为1*/
    UIView *back = [[UIView alloc]init];
    back.backgroundColor = self.backgroundColor;
    back.alpha = 0;
    [button.imageView addSubview:back];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(button.imageView);
    }];
    return button;
}

-(CALayer *)bottomLayer
{
    if (!_bottomLayer) {
        _bottomLayer = [[CALayer alloc]init];
        _bottomLayer.backgroundColor = [UIColor whiteColor].CGColor;
        UIButton *button = [self.btns objectAtIndex:0];
        _bottomLayer.frame = CGRectMake(button.width / 2 - 2.5, self.height - 12, 5, 5);
        _bottomLayer.cornerRadius = _bottomLayer.frame.size.height / 2;
        _bottomLayer.masksToBounds = YES;
    }
    return _bottomLayer;
}

-(NSMutableArray *)btns
{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

-(int)selectIndex
{
    return (int)[self.btns indexOfObject:self.selectBtn];
}
@end


